﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Domain;
using App.Core.Extensions;
using System.Reflection;


namespace App.Web.Utility
{
    public class BindSelectList
    {
        //public static SelectList Bind<T>(List<T> itemList, String dataValueField, String dataTextField)
        //    where T : class
        //{            
        //    SelectList selectList = new SelectList(itemList, dataValueField, dataTextField);
        //    SelectListItem item = new SelectListItem();            
        //    return selectList;            
        //}
        /// <summary>
        /// Binds a Collection to the Select List. Adds a new entry at first as (Please Select with blank value )
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="itemList"></param>
        /// <param name="dataValueField"></param>
        /// <param name="dataTextField"></param>
        /// <returns></returns>
        public static SelectList Bind<T>(List<T> itemList, String dataValueField, String dataTextField)
            where T : class
        {
            Type type = typeof(T);
            object obj = Activator.CreateInstance(type);
            PropertyInfo displayProperty = type.GetProperty(dataTextField);
            if (displayProperty == null)
                throw new ArgumentException(String.Format("Specified Data Text Field Property {0} was not found.", dataTextField));
            else
                displayProperty.SetValue(obj, "Please Select", null);

            displayProperty = type.GetProperty(dataValueField);
            if (displayProperty == null)
                throw new ArgumentException(String.Format("Specified Data Value Field Property {0} was not found.", dataValueField));
            else
                displayProperty.SetValue(obj, 0, null);

            if (itemList == null)
                itemList = new List<T>();

            itemList.Insert(0, (T)obj);
            SelectList selectList = new SelectList(itemList, dataValueField, dataTextField);
            return selectList;
        }

        public static SelectList Manufacturers()
        {
            AppEntities context = new AppEntities();
            List<Manufacturer> manufacturers = context.Manufacturers.OrderBy(P => P.Name).ToList();
            return Bind<Manufacturer>(manufacturers, "ID", "Name");
        }
        public static SelectList Categories()
        {
            AppEntities context = new AppEntities();
            List<Category> categories = (from P in context.Categories where P.PropertyDefinition != null select P).ToList(); 
            
            return Bind<Category>(categories, "ID", "Name");
        }
        public static SelectList ParentCategories()
        {
            AppEntities context = new AppEntities();
            List<Category> categories = (from P in context.Categories where P.ParentID == null select P).ToList(); 
            
            return Bind<Category>(categories, "ID", "Name");
        }
        public static SelectList IndustryTypes()
        {
            AppEntities context = new AppEntities();
            List<IndustryType> types = context.IndustryTypes.OrderBy(P => P.Name).ToList();
            return Bind<IndustryType>(types, "ID", "Name");
        }
        public static SelectList Countries()
        {
            AppEntities context = new AppEntities();
            List<Country> types = context.Countries.OrderBy(P => P.Name).ToList();
            return Bind<Country>(types, "ID", "Name");
        }
        public static SelectList Districts(int countryID)
        {
            AppEntities context = new AppEntities();
            List<District> types = (from P in context.Districts where P.CountryID == countryID orderby P.Name select P).ToList();
            return Bind<District>(types, "ID", "Name");
        }
        public static SelectList Gender()
        {
            var genderList = new SelectList(new[]
                            {
                                new {ID="",Name="Please Select"},
                                new {ID="M",Name="Male"},
                                new {ID="F",Name="Female"},
                                new {ID="O",Name="Other"},
                            }, "ID", "Name");  
            return genderList;
        }

        public static SelectList Religions()
        {
            AppEntities context = new AppEntities();
            List<Religion> types = (from P in context.Religions orderby P.Name select P).ToList();
            return Bind<Religion>(types, "ID", "Name");
        }

        public static SelectList PreferredPhone()
        {
            var phoneTypeList = new SelectList(new[]
                            {
                                new {ID="",Name="Please Select"},
                                new {ID="H",Name="Home"},
                                new {ID="M",Name="Mobile"},
                                new {ID="O",Name="Office"},
                            }, "ID", "Name");
            return phoneTypeList;
        }
        public static SelectList ProductQuality(String selectText = "")
        {
            if (selectText.IsNullOrEmpty())
                selectText = "Please Select";
            var phoneTypeList = new SelectList(new[]
                            {
                                new {ID="", Name = selectText},
                                new {ID="G", Name = "Good"},
                                new {ID="D", Name = "Damaged"},
                                new {ID="B", Name = "Bad"},
                            }, "ID", "Name");
            return phoneTypeList;
        }
        
        public static SelectList Products(int companyID)
        {
            AppEntities context = new AppEntities();
            List<Product> types = (from P in context.Products where P.CompanyID == companyID orderby P.Name select P).ToList();
            return Bind<Product>(types, "ID", "Name");
        }

        public static SelectList Stores(int companyID)
        {
            AppEntities context = new AppEntities();
            List<Store> types = (from P in context.Stores where P.CompanyID == companyID orderby P.Address select P).ToList();
            return Bind<Store>(types, "ID", "Address");
        }
        public static SelectList UOMs()
        {
            AppEntities context = new AppEntities();
            List<UOM> types = (from P in context.UOMs orderby P.Name select P).ToList();
            return Bind<UOM>(types, "ID", "Name");
        }
        public static SelectList CompanyUser(int CompanyID)
        {
            AppEntities context = new AppEntities();
            List<Contact> Contact = (from P in context.Contacts where P.CompanyID == CompanyID orderby P.LastName select P).ToList();
            return Bind<Contact>(Contact, "ID", "LastName");
        }
    }
}